<?php

class DefaultController extends Controller
{
    public function accessRules() {
        return array(
        );
    }
	public function actionIndex()
	{
		$this->render('index');
	}

    public function actionCreate(){
        //$emailList=array('@gmail.com','@hotmail.com','@facebook.com','@yahoo.com','@eggs.com','@ymail.com', '@apple.com');
        //echo $abc=$emailList[array_rand($emailList,1)]; die;
//       Yii::import('Business.models.*');
//        $businesses=Business::model()->findAll();
//        foreach($businesses as $business){
//            if($business->id==1) continue;
//            $bUser=new BusinessUser();
//
//            $bUser->business_id=$business->id;
//            $bUser->type=1;//TODO : hard code here. This should have a const value.
//            $bUser->employee_id=7;
//            $bUser->save();
////            $user=new User();
////            $user->username="user{$business->id}";
////            $user->email="user{$business->id}".$emailList[array_rand($emailList,1)];
////            $user->password='12345';
////            $user->address1="Ho Chi Minh, Viet Nam";
////            $user->address2="Brussels, Belgium";
////            $user->save(false);
//        }
//        echo "done";
    }
    public function actionEmployeeByService($service_id){
        //BusinessUser
        //$relations = array('services');
        $criteria=new CDbCriteria();
        $criteria->compare('service_id',$service_id);
        $userModels = ServiceAssignment::model()->findAll($criteria);
        $userModels=CHtml::listData($userModels,'employee_id','employee_id');
        //CVarDumper::dump($userModels,10,true);die;
        $models=array();
        if(count($userModels)){
            $criteria1=new CDbCriteria();
            $criteria1->addInCondition('id',array_values($userModels));
            $models=User::model()->findAll($criteria1);
        }
//CVarDumper::dump($userModels,10,true);die;
        //echo $json->toJSON($models, $relations);

        $this->sendResponse(200,CJSON::encode($models));
    }
    public function actionEmployee(){
        //TODO: We will have to update this code, because we are doing a sample function.
        $businessId=isset($_GET['business_id'])?$_GET['business_id']:1;
        /*Yii::import('application.behaviors.CArJSON');
        $json = new CArJSON();*/
        Yii::import('Business.models.BusinessUser') ;
        //BusinessUser
        //$relations = array('services');
        $criteria=new CDbCriteria();
        $criteria->compare('business_id',$businessId);
        $userModels = BusinessUser::model()->findAll($criteria);
        $userModels=CHtml::listData($userModels,'employee_id','employee_id');
        //CVarDumper::dump($userModels,10,true);die;
        $models=array();
        if(count($userModels)){
            $criteria1=new CDbCriteria();
            $criteria1->addInCondition('id',array_values($userModels));
            $models=User::model()->findAll($criteria1);
        }
//CVarDumper::dump($userModels,10,true);die;
        //echo $json->toJSON($models, $relations);

        $this->sendResponse(200,CJSON::encode($models));
    }
}